package com.ruoyi.kjy.service.impl;

import java.io.Serializable;
import java.util.*;

//import cn.easyes.core.biz.PageInfo;
//import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.Achievement;
import com.ruoyi.kjy.domain.Demand;
//import com.ruoyi.kjy.mapper.DemandEsMapper;
import com.ruoyi.kjy.mapper.DemandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kjy.service.IDemandService;

/**
 * 需求Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class DemandServiceImpl extends ServiceImpl<DemandMapper, Demand> implements IDemandService {
//    @Autowired
//    DemandEsMapper demandEsMapper;
    @Autowired
    DemandMapper demandMapper;
    @Override
    public boolean updateById(Demand entity) {
        //return super.updateById(entity) && demandEsMapper.updateById(entity) > 0;
        return super.updateById(entity);
    }

    //    @Override
//    public boolean removeById(Serializable id) {
//        demandEsMapper.deleteBatchIds(Arrays.asList(id));
//        return super.removeById(id) ;
//    }
    @Override
    public boolean removeByIds(Long[] ids) {
        List<Serializable> idList = Arrays.asList(ids);
        //demandEsMapper.deleteBatchIds(idList);
        return super.removeByIds(idList);
    }

    @Override
    public boolean save(Demand entity) {
        boolean save = super.save(entity);
        //Integer insert = demandEsMapper.insert(entity);
        return save;
    }
    @Override
    public IPage<Demand> page(int current, int size, Demand query) {
        Page<Demand> page = new Page<>(current, size);

        QueryWrapper<Demand> queryWrapper = new QueryWrapper<>();
        //queryWrapper.setEntity(query);

        String searchWord = query.getSearchWord();
        if (searchWord != null && !"".equals(searchWord)) {
            queryWrapper.like("title",searchWord);
        }
        List<String> location = query.getLocation();
        if (location != null && location.size() != 0) {
            //queryWrapper.in("location", location.get(0));
            queryWrapper.apply("location REGEXP {0}", location.get(0));
        }
        Integer state = query.getState();
        if (state != null) {
            queryWrapper.eq("state", state);
        }
        List<String> industrySector = query.getIndustrySector();
        if (industrySector != null && industrySector.size() != 0) {
            //queryWrapper.in("industry_sector", industrySector.get(0));
            queryWrapper.apply("industry_sector REGEXP {0}", industrySector.get(0));
        }
        Long author = query.getAuthor();
        if (author != null) {
            queryWrapper.eq("author", author);
        }queryWrapper.orderByDesc("create_time");
        IPage<Demand> resultPage = demandMapper.selectPage(page, queryWrapper);

        return resultPage;
    }
//    @Override
//    public PageInfo<Demand> espage(Integer current, Integer size, Demand demand) {//用于推荐时，不推id相同的
//        LambdaEsQueryWrapper<Demand> lambdaEsQueryWrapper = new LambdaEsQueryWrapper<>();
//        String searchWord = demand.getSearchWord();
//        if (searchWord != null && !"".equals(searchWord)) {
//            lambdaEsQueryWrapper.queryStringQuery(searchWord);
//        }
//        List<String> location = demand.getLocation();
//        if (location!=null && location.size() != 0){
//            lambdaEsQueryWrapper.matchPhrase("location", location.get(0));
//        }
//        Integer state = demand.getState();
//        if (state != null) {
//            lambdaEsQueryWrapper.matchPhrase("state", state);
//        }
//        List<String> industrySector = demand.getIndustrySector();
//        if (industrySector!=null && industrySector.size() != 0){
//            lambdaEsQueryWrapper.matchPhrase("industrySector", industrySector.get(0));
//        }
//        Long author = demand.getAuthor();
//        if (author != null) {
//            lambdaEsQueryWrapper.matchPhrase("author", author);
//            lambdaEsQueryWrapper.orderByDesc(Demand::getCreateTime);
//        }
//        PageInfo<Demand> demandPageInfo=new PageInfo<Demand>();;
//        try{
//            demandPageInfo = demandEsMapper.pageQuery(lambdaEsQueryWrapper, current, size);
//        }finally {
//            return demandPageInfo;
//        }
//    }

    @Override
    public void updateNickName(Demand demand) {
//        LambdaEsQueryWrapper<Demand> lambdaEsQueryWrapper = new LambdaEsQueryWrapper<>();
//        lambdaEsQueryWrapper.matchPhrase("author", demand.getAuthor());
//        List<Demand> demands = demandEsMapper.selectList(lambdaEsQueryWrapper);
//        for (Demand demand1 : demands) {
//            demand1.setAuthorName(demand.getAuthorName());
//        }
//        demandEsMapper.updateBatchByIds(demands);

        QueryWrapper<Demand> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("author",demand.getAuthor());
        List<Demand> demands= this.list(queryWrapper);
        for (Demand item : demands) {
            item.setAuthorName(demand.getAuthorName());
        }
        this.updateBatchById(demands);
    }
}
